//
//  YWNewMessageSettingViewController.m
//  maintain
//
//  Created by Kyo on 15/11/2.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWNewMessageSettingViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWPushManager.h"

CGFloat const kCellRowHeight = 44.0f;
CGFloat const kTableViewTopInset = 20.0f;
CGFloat const kTableViewHeightForFooter = 70.0f;

@interface YWNewMessageSettingViewController () <UITableViewDataSource, UITableViewDelegate> {
}
@property (nonatomic, strong) NSArray* titles;
@end

@implementation YWNewMessageSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"newMessage", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.automaticallyAdjustsScrollViewInsets = NO;

    [self initTableView];
}

- (NSArray*)titles
{
    if (!_titles) {
        NSString *receiveNewMessage = NSLocalizedString(@"receiveNewMessage", nil);
        NSArray* titles = @[ @[ receiveNewMessage ]];
        _titles = titles;
    }
    return _titles;
}

- (void)initTableView
{
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor customBackgroudColor];
    tableView.separatorColor = [UIColor customLineColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = kCellRowHeight;
    UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = kTableViewTopInset;
    [tableView setContentInset:contentInset];
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc] init];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return [self.titles count];
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.titles[section] count];
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return kTableViewHeightForFooter;
}
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, kTableViewHeightForFooter)];
    if (section == 0) {
        UILabel* prompt = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, tableView.frame.size.width - 30, kTableViewHeightForFooter)];
        prompt.textAlignment = NSTextAlignmentLeft;
        prompt.numberOfLines = 3;
        prompt.font = [UIFont systemFontOfSize:15.0f];
        [prompt setTextColor:[UIColor customTextGrayColor]];
        prompt.text = NSLocalizedString(@"receiveNewMessageMethod", nil);
        [view addSubview:prompt];
    }
    else if (section == 1) {
//        UILabel* prompt = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, tableView.frame.size.width - 30, 50)];
//        prompt.textAlignment = NSTextAlignmentLeft;
//        prompt.numberOfLines = 2;
//        prompt.font = [UIFont systemFontOfSize:15.0f];
//        [prompt setTextColor:[UIColor customTextGrayColor]];
//        prompt.text = @"当易维保运行时，你可以设置是否需要声音或者震动";
//        [view addSubview:prompt];
    }

    return view;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    static NSString* cellIdentifier = @"BZNewMessageSettingCell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.font = KTitleFont;
        cell.detailTextLabel.font = KTitleFont;
        cell.detailTextLabel.textColor = [UIColor customGreenColor];
    }
    cell.textLabel.text = self.titles[indexPath.section][indexPath.row];
    switch (indexPath.section) {
    case 0: {
        if ([[YWPushManager shareManager] openNotifyAlert]) {
            cell.detailTextLabel.text = NSLocalizedString(@"isOpen", nil);
        }
        else {
            cell.detailTextLabel.text = NSLocalizedString(@"isClose", nil);
        }
    }
            if (iOS8) {//这个判断不能删，ios才可以点击这个cell，所以有个箭头
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
        break;
    case 1: {
        UISwitch* switchBtn = [[UISwitch alloc] init];
        [switchBtn addTarget:self action:@selector(handleSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];

        if (indexPath.row == 0) {
            switchBtn.tag = 13;
            //                switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:KSettingMessageSound];
            cell.accessoryView = switchBtn;
        }
        else if (indexPath.row == 1) {
            switchBtn.tag = 14;
            //                switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:KSettingMessageVibrate];
            cell.accessoryView = switchBtn;
        }
    }
    default:
        break;
    }

    return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {
        if (iOS8) {
            NSURL* url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:url];
            return;
        }
    }
}
#pragma mark - Private APIs

- (void)handleSwitchValueChanged:(UISwitch*)sender
{

    //    switch (sender.tag) {
    //        case 13:
    //            [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:KSettingMessageSound];
    //
    //            break;
    //        case 14:
    //            [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:KSettingMessageVibrate];
    //            break;
    //        default:
    //            break;
    //    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
